package com.zqj.project.converter;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zqj.project.dto.OrderDTO;
import com.zqj.project.entity.OrderPO;
import com.zqj.project.model.Order;
import com.zqj.project.vo.OrderVO;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description: OrderConverter
 * @Author: 22001010704 Zhang Qijian
 * @CreateTime: 2024/12/8 20:53
 * @Version: 1.0
 */
public class OrderConverter {

    private OrderConverter() {
        throw new IllegalStateException("Utility Class");
    }

    public static OrderVO toOrderVo(Order order) {
        return BeanUtil.toBean(order, OrderVO.class);
    }

    public static Order toOrderModel(OrderDTO orderDto) {
        return BeanUtil.toBean(orderDto, Order.class);
    }

    public static Order toOrderModel(OrderPO orderPo) {
        return BeanUtil.toBean(orderPo, Order.class);
    }

    public static OrderPO toOrderPo(Order order) {
        return BeanUtil.toBean(order, OrderPO.class);
    }

    public static List<OrderVO> toOrderVoList(List<Order> orderModelList) {
        if (CollUtil.isEmpty(orderModelList)) {
            return Collections.emptyList();
        }
        return orderModelList.stream().map(e -> BeanUtil.toBean(e, OrderVO.class)).toList();
    }

    public static List<Order> toOrderModelList(List<OrderPO> orderPoList) {
        if (CollUtil.isEmpty(orderPoList)) {
            return Collections.emptyList();
        }
        return orderPoList.stream().map(e -> BeanUtil.toBean(e, Order.class)).toList();
    }

    public static IPage<OrderVO> toOrderVoPage(IPage<Order> orderModelPage) {
        IPage<OrderVO> orderVoPage = new Page<>(orderModelPage.getCurrent(), orderModelPage.getSize());
        List<OrderVO> orderVoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(orderModelPage.getRecords())) {
            orderModelPage.getRecords().forEach(entity -> orderVoList.add(BeanUtil.toBean(entity, OrderVO.class)));
            orderVoPage.setRecords(orderVoList);
            orderVoPage.setTotal(orderModelPage.getTotal());
        }
        return orderVoPage;
    }

}